const mysql = require('mysql2/promise');

async function checkTableStructure() {
  console.log('🔍 检查数据库表结构...\n');

  const connection = await mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'community_service_platform'
  });

  try {
    // 检查services表结构
    console.log('📋 检查services表结构:');
    const [columns] = await connection.execute('DESCRIBE services');
    console.log('字段列表:');
    columns.forEach(col => {
      console.log(`  ${col.Field} (${col.Type}) - ${col.Null === 'YES' ? '可空' : '非空'} - ${col.Key || '无键'}`);
    });

    console.log('\n📋 检查services表数据:');
    const [rows] = await connection.execute('SELECT * FROM services LIMIT 3');
    console.log('数据示例:');
    rows.forEach((row, index) => {
      console.log(`  记录${index + 1}:`, JSON.stringify(row, null, 2));
    });

    // 检查service_categories表结构
    console.log('\n📋 检查service_categories表结构:');
    const [categoryColumns] = await connection.execute('DESCRIBE service_categories');
    console.log('字段列表:');
    categoryColumns.forEach(col => {
      console.log(`  ${col.Field} (${col.Type}) - ${col.Null === 'YES' ? '可空' : '非空'} - ${col.Key || '无键'}`);
    });

    console.log('\n📋 检查service_categories表数据:');
    const [categoryRows] = await connection.execute('SELECT * FROM service_categories LIMIT 3');
    console.log('数据示例:');
    categoryRows.forEach((row, index) => {
      console.log(`  记录${index + 1}:`, JSON.stringify(row, null, 2));
    });

  } catch (error) {
    console.error('❌ 检查表结构失败:', error.message);
  } finally {
    await connection.end();
  }

  console.log('\n🎉 表结构检查完成！');
}

checkTableStructure().catch(console.error); 